package com.open.party.cloud.view.home.dangWuMiShu

import android.os.Bundle
import com.sinothk.android.utils.inters.OnSuperListener
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.TouPiaoGuanBean
import com.open.party.cloud.model.TouPiaoGuanVo
import com.open.party.cloud.view.home.dangWuMiShu.adapter.TouPiaoGuanLiListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DwmsTouPiaoGuanLiListActivity : AppEventBusRecycleViewTitleBaseActivity<TouPiaoGuanVo>(), OnSuperListener<TouPiaoGuanBean> {

    private var viewModel: BizViewModel? = null
    private var adapter: TouPiaoGuanLiListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("投票管理")

        viewModel = BizViewModel()

        initView()
        refreshData()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = TouPiaoGuanLiListAdapter(this)
        loadingRecyclerView.adapter = adapter
        loadingRecyclerView.setLoadingMoreEnabled(false)
        adapter!!.setOnSuperListener(this)

        loadingTipView.showLoading("努力加载中")
        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }

    override fun loadData(pageVo: PageVo<TouPiaoGuanVo>) {
        val pageReq = PageReq<TouPiaoGuanVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize
        val vo = TouPiaoGuanVo()
        pageReq.data = vo
        viewModel!!.getTouPiaoGuanListData(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<TouPiaoGuanBean>>) {
        if (result.eventType != "getTouPiaoGuanListData") {
            return
        }
        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<TouPiaoGuanBean>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    override fun onClick(position: Int, itemData: TouPiaoGuanBean, flag: String?) {
        if (XUtils.string().isEmpty(itemData.id)) {
            XUtils.toast().show("数据异常")
            return
        }

        XUtils.intent().openActivity(this@DwmsTouPiaoGuanLiListActivity, DwmsTouPiaoGuanLiDetailActivity::class.java)
            .putStringExtra("id", itemData.id)
            .start()
    }
}